<template>
  <div>
    <div class="top">
      <el-button
        type="primary"
        class="el-icon-plus add"
        size="mini"
        @click="materialEntry"
        >入库</el-button
      >
      <el-form
        ref="form"
        :model="formData"
        label-width="80px"
        inline
        size="mini"
      >
        <el-form-item>
          <el-input
            v-model="formData.materialName"
            placeholder="请输入物料名称"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-select
            v-model="formData.materialType"
            placeholder="请选择物料类型"
            clearable
          >
            <el-option
              :label="item.label"
              :value="item.label"
              v-for="(item, index) in materialType"
              :key="index"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="查询时间">
          <el-date-picker
            v-model="time"
            type="daterange"
            align="right"
            unlink-panels
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            :picker-options="pickerOptions"
            value-format="yyyy-MM-dd HH:mm:ss"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="">
          <el-button type="primary" @click="search">查询</el-button>
        </el-form-item>
      </el-form>
    </div>
    <el-table :data="tableData" border>
      <el-table-column
        :prop="item.prop"
        :label="item.label"
        :width="item.width"
        align="center"
        v-for="(item, index) in materialEntryTableHeard"
        :key="index"
      >
      </el-table-column>
      <!-- 无数据 -->
      <template slot="empty">
        <div class="noData" v-show="tableData.length === 0">
          <img src="@/assets/images/noData.png" />
          <p>暂无数据</p>
        </div>
      </template>
    </el-table>
    <Pagination :total="total" @pagination="handlePage" :page.sync="formData.pageNum"></Pagination>

    <el-dialog title="新增入库" :visible.sync="materialDialog" width="40%">
      <div>
        <el-row>
          <el-form
            ref="materialForm"
            label-width="80px"
            :model="materialForm"
            size="mini"
            :rules="materialRules"
          >
            <el-col :span="12">
              <el-form-item label="物料名称" prop="materialId">
                <el-select
                  v-model="materialForm.materialId"
                  placeholder="请选择物料"
                  @change="selectMaterialName"
                >
                  <el-option
                    :label="item.name"
                    :value="item.id"
                    v-for="item in materialList"
                    :key="item.id"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="规格" prop="specification">
                <el-input
                  :disabled="true"
                  v-model="materialForm.specification"
                  placeholder="请输入物料规格"
                ></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="入库类型" prop="storageType">
                <el-select
                  v-model="materialForm.storageType"
                  placeholder="请选择入库类型"
                >
                  <el-option
                    :label="item"
                    :value="item"
                    v-for="(item, index) in entryType"
                    :key="index"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="入库数量" prop="storageNumber">
                <el-input
                  v-model="materialForm.storageNumber"
                  placeholder="请输入物入库数量"
                ></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="物料批次" prop="batchNumber">
                <el-input
                  v-model="materialForm.batchNumber"
                  placeholder="请输入物料批次"
                ></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="批次单价" prop="batchPrice">
                <el-input
                  v-model="materialForm.batchPrice"
                  placeholder="请输入批次单价"
                >
                  <p slot="suffix" style="display: inline">元</p>
                </el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="入库日期" prop="storageDate">
                <el-date-picker
                  style="width: 160px"
                  v-model="materialForm.storageDate"
                  type="date"
                  placeholder="选择日期"
                  :picker-options="pickerOptionsForm"
                  value-format="yyyy-MM-dd HH:mm:ss"
                >
                </el-date-picker>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="生产日期" prop="dateOfManufacture">
                <el-date-picker
                  style="width: 160px"
                  v-model="materialForm.dateOfManufacture"
                  type="date"
                  placeholder="选择日期"
                  :picker-options="pickerOptionsForm"
                  value-format="yyyy-MM-dd HH:mm:ss"
                >
                </el-date-picker>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="供应商" prop="supplierId">
                <el-select
                  v-model="materialForm.supplierId"
                  placeholder="请选择供应商"
                >
                  <el-option
                    :label="item.name"
                    :value="item.id"
                    v-for="item in supplierList"
                    :key="item.id"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
          </el-form>
        </el-row>
      </div>
      <div slot="footer">
        <el-button @click="materialDialog = false">取 消</el-button>
        <el-button type="primary" @click="confirm">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { materialEntryTableHeard } from "./constant/index.js";
import { materialList } from "@/api/material-management/material-list";
import { storageList } from "@/api/material-management/material-entry";
import { mixins } from "./mixin/index.js";
export default {
  mixins: [mixins],
  data() {
    return {
      total: 0,
      materialDialog: false,
      pickerOptions: {
        shortcuts: [
          {
            text: "最近一周",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
              picker.$emit("pick", [start, end]);
            },
          },
          {
            text: "最近一个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
              picker.$emit("pick", [start, end]);
            },
          },
          {
            text: "最近三个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
              picker.$emit("pick", [start, end]);
            },
          },
        ],
      },
      pickerOptionsForm: {
        disabledDate(time) {
          return time.getTime() > Date.now() - 8.64e6;
        },
      },
      time: [],
      formData: {
        materialName: "",
        materialType: "",
        storageTimeStart: "",
        storageTimeEnd: "",
        pageNum: 1,
        pageSize: 10,
      },
      materialForm: {
        materialId: null,
        specification: "",
        storageDate: "",
        storageType: "",
        storageNumber: "",
        batchNumber: "",
        batchPrice: "",
        dateOfManufacture: "",
        supplierId: null,
      },
      tableData: [],
      materialEntryTableHeard,
      materialList: [],
      supplierList: [],
      initForm: {},
    };
  },
  created() {
    this.getMaterialList();
    this.getTableData();
    this.initForm = this.$cloneDeep(this.materialForm);
  },
  methods: {
    //物料列表
    async getMaterialList() {
      let res = await materialList({
        expiryReminders: "",
        id: null,
        name: "",
        pageNum: 0,
        pageSize: 10,
        shelfLife: "",
        specification: "",
        type: "",
        unitOfMeasure: "",
        unitPrice: 0,
      });
      if (res.code == 200) {
        this.materialList = res.data.list;
      }
    },
    async getTableData() {
      let res = await storageList(this.formData);
      if (res.code == 200) {
        this.tableData = res.data.list;
        this.total = res.data.total;
      }
    },
    materialEntry() {
      this.materialDialog = true;
      this.isDateOfManufacture = false;
      this.materialForm = this.$cloneDeep(this.initForm);
      this.$nextTick(() => {
        this.$refs.materialForm.clearValidate();
      });
    },
    //选择物料时自动获取规格
    selectMaterialName(event) {
      let selectMaterial = this.materialList.filter((item) => {
        return item.id === event;
      });
      this.materialForm.specification = selectMaterial[0].specification;
    },
    search() {
      this.formData.pageNum = 1
      this.getTableData()
    }
  },
  watch: {
    time(newValue) {
      if (newValue) {
        this.formData.storageTimeStart = newValue[0];
        this.formData.storageTimeEnd = newValue[1];
      } else {
        this.formData.storageTimeStart = "";
        this.formData.storageTimeEnd = "";
      }
    },
  },
  
};
</script>

<style></style>
